package com.itzmn.java.javase.io.bio;

import java.io.*;

/**
 * @Author: 张梦楠
 * @Date: 2018/12/25 17:00
 * 简书：https://www.jianshu.com/u/d611be10d1a6
 * 码云：https://gitee.com/zhangqiye
 * @Description: BIO模式下的文件读写
 */
public class FileIO {


    public static void main(String[] args) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File file = new File("test.txt");
            inputStream = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len = -1;
            while ( (len = inputStream.read(bytes)) > 0 ){
                String s = new String(bytes, 0, len);
                System.out.println(s);
            }
            System.out.println("读取完毕");
            /**
             * true表示写入数据是追加，默认false是覆盖
             */
            outputStream = new FileOutputStream(file,true);
            outputStream.write("你好".getBytes());
            outputStream.flush();
            System.out.println("写入完毕");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
